<?php

namespace App\Models;

use Cog\Contracts\Love\Likeable\Models\Likeable as LikeableContract;
use Cog\Laravel\Love\Likeable\Models\Traits\Likeable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Parsedown;

/**
 * Class Chapter
 * @package App\Models
 *
 * @method \Illuminate\Database\Eloquent\Builder indexQuery()
 */
class Chapter extends Model implements LikeableContract
{
    use SoftDeletes;
    use Likeable;
    protected static $unguarded = true;

    const STATUS_NORMAL = 0;

    const STATUS_ADULT = 1;//含有成人内容

    const STATUS_ILLEGAL = 2;//非法内容

    const TYPE_SIMPLE = 0;

    const TYPE_MARKDOWN = 1;

    const TYPE_RICHTEXT = 2;//富文本

    protected $fillable = ['title', 'content', 'status', 'type'];

    protected $hidden = ['content'];

    protected $appends = ['content_html', 'liked', 'likes_count', 'disliked', 'dislikes_count'];

    protected $touches = ['repositories'];

    protected $with = ['user', 'comments', 'comments.user'];

    public function scopeNextSequenceNumber($query, $id)
    {
        return $query->where('repository_id', $id)->max('sequence') + 1;
    }

    public function repositories()
    {
        return $this->belongsToMany(Repository::class)->wherePivot('is_using', 1)->withPivot(['sequence', 'is_using']);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function pulls()
    {
        return $this->belongsToMany(Repository::class, 'pulls');
    }

    public function getContentHtmlAttribute()
    {
        if ($this->status == static::STATUS_ILLEGAL) {
            return null;//可能会在创建副本后更新章节时会显示
        }
        switch ($this->type) {
            case static::TYPE_MARKDOWN:
                $Parsedown = new Parsedown();
                $Parsedown->setSafeMode(true);
                $html = $Parsedown->text($this->content);
                break;
            case static::TYPE_RICHTEXT:
                $html = $this->content;
                break;
            default:
                $html = nl2p($this->content);
        }
        return $html;
    }
    public function getContentDownloadAttribute()
    {
        if ($this->status == static::STATUS_ILLEGAL) {
            return null;//可能会在创建副本后更新章节时会显示
        }
        switch ($this->type) {
            case static::TYPE_MARKDOWN:
                // $Parsedown = new Parsedown();
                // $Parsedown->setSafeMode(true);
                // $html = $Parsedown->text($this->content);
                // break;
            case static::TYPE_RICHTEXT:
            default:
                $html = $this->content;
        }
        return $html;
    }
    public function scopeIndexQuery($query)
    {
        return $query->with(['repositories' => function ($q) {
            return $q->where('type', '!=', Repository::TYPE_HIDDEN);
        }])->latest();
    }

    public function sequence()
    {
        return $this->repositories()->first()->pivot->sequence;
    }
}